package com.cuiweiyou.bluemesh.model;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import java.io.Serializable;

public class LightModel implements Serializable, Comparable<LightModel> {

    private int rssi;    // 信号强度
    private String mile; // 信号强度转换的距离
    private double avgMile; // 信号强度转换的距离
    private String name;    // 设备名，即mesh组名称
    private String address; // MAC地址
    private String nickname;  // 用户自定义的别称
    private long lastLiveTime;   // 上次发现时间
    private boolean isAdmi = false; // 是否是组控制器-小头目
    private boolean isOpen = false; // 是否开灯
    private boolean isOnline = false; // 是否搜索到
    private BluetoothDevice device;

    public LightModel() {
    }

    public BluetoothDevice getDevice() {
        return device;
    }

    public void setDevice(BluetoothDevice device) {
        this.device = device;
    }

    public int getRssi() {
        return rssi;
    }

    public void setRssi(int rssi) {
        this.rssi = rssi;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * 是否打开了
     *
     * @return
     */
    public boolean isOpen() {
        return isOpen;
    }

    /**
     * 是否打开了
     *
     * @param open
     */
    public void setOpen(boolean open) {
        isOpen = open;
    }

    /**
     * 是否是管理员
     *
     * @return
     */
    public boolean isAdmi() {
        return isAdmi;
    }

    /**
     * 是否是管理员
     *
     * @param admi
     */
    public void setAdmi(boolean admi) {
        isAdmi = admi;
    }

    public boolean isOnline() {
        return isOnline;
    }

    public void setOnline(boolean online) {
        isOnline = online;
    }

    /**
     * 上次扫描到的时间
     *
     * @return
     */
    public long getLastLiveTime() {
        return lastLiveTime;
    }

    /**
     * 上次扫描到的时间
     *
     * @param lastLiveTime
     */
    public void setLastLiveTime(long lastLiveTime) {
        this.lastLiveTime = lastLiveTime;
    }

    /**
     * 距离
     *
     * @return
     */
    public String getMile() {
        return mile;
    }

    public void setMile(double mileD) {
        this.mile = String.format("%.2f", mileD);
        avgMile = (avgMile + mileD) / 2; // 会不会扫描到次数少的看起来更近？
    }

    /**
     * 历次扫描到的距离平均值
     */
    public double getAvgMile() {
        return avgMile;
    }

    /**
     * 灯具别称：如"北屋"、"客厅"
     *
     * @return
     */
    public String getNickname() {
        return null != nickname ? nickname : address;
    }

    /**
     * 灯具别称：如"北屋"、"客厅"
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "LightModel{" + "rssi=" + rssi + ", mile='" + mile + '\'' + ", avgMile='" + avgMile + '\'' + ", name='" + name + '\'' + ", address='" + address + '\'' + ", nickname='" + nickname + '\'' + ", lastLiveTime=" + lastLiveTime + ", isAdmi=" + isAdmi + ", isOpen=" + isOpen + ", isOnline=" + isOnline + ", device=" + device + '}';
    }

    @Override
    public boolean equals(Object obj) {
        LightModel tmp = (LightModel) obj;
        String addressTmp = tmp.getAddress();
        String address = this.getAddress();
        return addressTmp.equals(address);
    }

    @Override
    public int compareTo(LightModel light) {
        String address1 = light.getAddress();
        String address2 = this.getAddress();
        return address1.compareToIgnoreCase(address2);
    }
}
